//
//  CWHomeController.swift
//  CWWeibo06
//
//  Created by ycw on 2017/7/21.
//  Copyright © 2017年 xiaowei. All rights reserved.
//

import UIKit
import YYModel



class CWHomeController: CWVisibleController {
    //懒加载上拉刷新的控件
    fileprivate lazy var pullUpView: UIActivityIndicatorView = {
       let activity = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.white)
        activity.color = UIColor.red
        return activity
    }()
    //懒加载系统下拉刷新的控件
    fileprivate lazy var pullDownView: CWRefreshControl = {
        let refresh: CWRefreshControl = CWRefreshControl()
        //refresh.addTarget(self, action: #selector(didPullDownRefreshControl), for: .valueChanged)
        return refresh
    }()
    
    //通过viewmodel引用数据加载
    fileprivate lazy var homeViewModel: CWHomeViewModel = CWHomeViewModel()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        if !isLogin {
            visibleView?.updateVisibleViewInfo(labelText: nil, imageName: nil)
        }
        setupUI()
        
        loadData()
    }
    
    private func setupUI() {
        
        tableView.register(CWHomeCell.self, forCellReuseIdentifier: homeCellID)

        tableView.estimatedRowHeight = 200
        
        tableView.rowHeight = UITableViewAutomaticDimension
        //去掉分割线
        tableView.separatorStyle = .none
        
        tableView.tableFooterView = pullUpView
        
        tableView.addSubview(pullDownView)
        
        
    }
    //MARK: 下拉刷新执行的方法
    @objc fileprivate func didPullDownRefreshControl() {
        
        homeViewModel.loadData(isUp: pullUpView.isAnimating) { (isSuccess: Bool) in
            
            self.endRefreshing()
            if isSuccess {
                
                self.tableView.reloadData()
            }
        }
    }
    
    
    
    //MARK: 加载数据
    fileprivate func loadData() {
        
        //通过判断上拉刷新控件是否在动画判断是上拉还是下拉
        homeViewModel.loadData(isUp: pullUpView.isAnimating) { (isSuccess: Bool) in
            
            self.endRefreshing()
            if isSuccess {
                
                self.tableView.reloadData()
            }
        }
    }

    //结束刷新的方法
    fileprivate func endRefreshing() {
        pullUpView.stopAnimating()
        //pullDownView.endRefreshing()
    }
}

extension CWHomeController {
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        
        return 1
        
    }
    
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return homeViewModel.statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: homeCellID, for: indexPath) as! CWHomeCell
        cell.statusViewModel = homeViewModel.statusList[indexPath.row]
        return cell
        
    }
    
    override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        if indexPath.row == homeViewModel.statusList.count - 1 && !pullUpView.isAnimating{
            pullUpView.startAnimating()
            loadData()
        }
    }
    
}










